#!/bin/bash
##
# Copyright (c) Microsoft Corporation.  All rights reserved.
#
# Contains settings for the OMI WS-Management Deamon.
#
#

### BEGIN INIT INFO
# Provides:          omid
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: OMI Server
# Description:       Microsoft Open Management Infrastructure (OMI) Server
### END INIT INFO

OMI_HOME=/opt/omi
OMI_NAME="Microsoft OMI Server"

OMI_BIN=$OMI_HOME/bin/omiserver
test -x $OMI_BIN || { echo "$OMI_BIN not installed";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 5; fi; }

# PID File
PIDFILE=/var/opt/omi/run/omiserver.pid

if [ -f /etc/init.d/functions ]; then
    INIT_STYLE=R      # INIT_STYLE uses R/S/D for its representative platform RedHat/SuSE/Debian 
    . /etc/init.d/functions
elif [ -f /etc/rc.status ]; then
    INIT_STYLE=S
    . /etc/rc.status && rc_reset
elif [ -f /lib/lsb/init-functions ]; then
    INIT_STYLE=D
    . /lib/lsb/init-functions
else
    echo -n "Could not source init functions."
    exit 1
fi

RETVAL=0
case "$1" in
     start)
        LD_LIBRARY_PATH=$OMI_HOME/lib; export LD_LIBRARY_PATH
        CREATE_LINKS=/opt/omi/bin/support/installssllinks

        case $INIT_STYLE in
            D) 
                log_begin_msg "Starting $OMI_NAME: "
                $CREATE_LINKS && start-stop-daemon --start --quiet --pidfile $PIDFILE --name "omid" --startas $OMI_BIN -- --configfile=/etc/opt/omi/conf/omiserver.conf -d
                RETVAL=$?
                log_end_msg $RETVAL
                ;;               
            R) 
                echo -n "Starting $OMI_NAME: "
                $CREATE_LINKS && daemon $OMI_BIN --configfile=/etc/opt/omi/conf/omiserver.conf -d
                RETVAL=$?
                echo
                ;;
            S) 
                . /etc/sysconfig/language
                unset LC_ALL
                LC_CTYPE="$RC_LANG"; export LC_CTYPE
                echo -n "Starting $OMI_NAME "
                $CREATE_LINKS && startproc -p $PIDFILE $OMI_BIN --configfile=/etc/opt/omi/conf/omiserver.conf -d
                rc_status -v
                ;;
            *)  exit 1   ;;
        esac  ;;
     stop)
        LD_LIBRARY_PATH=$OMI_HOME/lib; export LD_LIBRARY_PATH
        case $INIT_STYLE in
            D)
                log_begin_msg "Shutting down $OMI_NAME: "
                $OMI_BIN -s > /dev/null 2>&1
                RETVAL=$?
                log_end_msg $RETVAL
                ;;
            R)
                echo -n "Shutting down $OMI_NAME: "
                $OMI_BIN -s > /dev/null 2>&1
                RETVAL=$?
                if [ $RETVAL -eq 0 ]; then echo_success; else echo_failure; fi
                echo
                ;;
            S)
                echo -n "Shutting down $OMI_NAME: "
                $OMI_BIN -s > /dev/null 2>&1
                killproc -p $PIDFILE $OMI_BIN
                rc_status -v
                ;;
            *)  exit 1    ;;
        esac
        ;;
     restart)
        $0 stop
        $0 start
        case $INIT_STYLE in
            S)   rc_status  ;;
          D|R)   RETVAL=$?  ;;
            *)   exit 1     ;;
        esac
        ;;
     status)
        echo -n "Checking for service $OMI_NAME "
        case $INIT_STYLE in
            D)
                status_of_proc $OMI_BIN
                RETVAL=$?
                ;;
            R)
                status $OMI_BIN
                RETVAL=$?
                ;;
            S)
                checkproc -p $PIDFILE $OMI_BIN
                rc_status -v
                ;;
            *)  exit 1    ;;
        esac
        ;;
     *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac
if [ $INIT_STYLE == S ]; then rc_exit; else exit $RETVAL; fi
